﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VSSAHT.DataAccess;
using VSSAHT.Transfer;

namespace VSS_AHT
{
    public partial class FormUpdateUser : BaseForm
    {
        #region Ctors
        public FormUpdateUser()
        {
            InitializeComponent();
            Initialize();
        }
        private void Initialize()
        {
            this.MaximizeBox = false;

            
            this.btnSave.Click += new EventHandler(delegate { ClickButtonSave(); });
            this.btnCancel.Click += new EventHandler(delegate { this.Close(); });
        }

        #endregion

        #region Property Member
        private bool _isEdit = false;
        private bool _isBinding = false;
        private UserDTO _user = new UserDTO();
        UserDataAccess _dataAccess = new UserDataAccess();

        RoleDataAccess _dataAccessRole = new RoleDataAccess();
        IList<RoleDTO> _roleList;
        public UserDTO User
        {
            get { return _user; }
            set
            {
                if (_user != value)
                {
                    _isEdit = true;
                    _user = value;
                    this.txtPassword.Focus();
                }
            }
        }

        private IList<UserDTO> _collection;
        public IList<UserDTO> Collection
        {
            set { _collection = value; }
        }
        #endregion

        #region Methods

        private void ClickButtonSave()
        {
            bool invalidInput = false;

            foreach (Control ctrl in this.Controls)
            {
                if (errorProvider.GetError(ctrl) != String.Empty)
                {
                    invalidInput = true;
                    break;
                }
            }

            if (invalidInput)
            {
                Common.ShowAlert("Thông tin bạn nhập chưa hợp lệ, vui lòng kiểm tra lại");
            }
            else
            {
                this.errorProvider.Clear();

                bool result = false;
                if (string.Compare(txtPassword.Text, txtRetypePassword.Text) != 0)
                {
                    Common.ShowAlert("Password nhập không khớp, xin vui lòng nhập lại!");
                    txtPassword.Text = "";
                    txtRetypePassword.Text = "";
                    txtPassword.Focus();
                }
                else
                {
                    _user.Username = txtUserName.Text;
                    _user.PasswordEncrypt = Security.Encrypt(txtPassword.Text);
                    _user.RoleID = (Guid)this.lookUpRole.EditValue;
                    if (!_isEdit)
                    {
                        result = _dataAccess.Add(_user);

                        if (result)
                        {
                            _collection.Add(_user);
                            _user = new UserDTO();
                            this.txtUserName.Text = "";
                            this.txtPassword.Text = "";
                            this.txtRetypePassword.Text = "";
                            this.txtUserName.Focus();
                        }
                        else
                        {
                            Common.ShowAlert("Tạo mới thông tin người dùng không thành công!");
                        }
                    }
                    else
                    {
                        result = _dataAccess.Edit(_user);

                        if (result)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            Common.ShowAlert("Chỉnh sửa thông tin người dùng không thành công!");
                        }

                    }
                }
            }
        }

        

        

        
        #endregion
      protected override void OnLoad(EventArgs e)
        {
          //Initiallize Role List
            _dataAccessRole.SearchObject = new RoleDTO();
            _dataAccessRole.SearchObject.RoleName = "";
            _roleList = _dataAccessRole.Search();
            this.lookUpRole.Properties.DataSource = _roleList; ;
            this.lookUpRole.Properties.DisplayMember = "RoleName";
            this.lookUpRole.Properties.ValueMember = "RoleID";
            this.lookUpRole.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;
            if (_isEdit)
            {
                this.txtUserName.Enabled = false;
                this.txtUserName.Text = User.Username;
                this.lookUpRole.EditValue = User.RoleID;
                //if (User.GroupID == 1)
                //{
                //    rdoAdmin.Checked = true;
                //}
                //else
                //{
                //    rdoUser.Checked = true;
                //}
            }
            else
            {
                this.txtUserName.Enabled = true;
                this.lookUpRole.ItemIndex = 0;
            }

          //load Role
           
           
            base.OnLoad(e);
        }
    }
}